package com.dream.system.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dream.common.JsonString;
import com.dream.common.Msg;
import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.model.Sys_Class;
import com.dream.model.Sys_ClassPlantMaster;
import com.dream.model.Sys_Lable;
import com.dream.model.Sys_LearnPlant;
import com.dream.model.Sys_TestPaper;
import com.dream.system.service.ISysClass;
import com.dream.system.service.ISysClassPlant;
import com.dream.system.service.ISysLable;
import com.dream.system.service.ISysUser;
import com.dream.system.service.ISys_LearnPlant;

@Controller
@RequestMapping("/edu/sys_LearnPlantAction.do")
@SuppressWarnings("all")
public class Sys_LearnPlantAction extends SuperController {

	@Autowired
	private ISysUser sysUserImpl;
	@Autowired
	private ISysClass sysClassServiceImpl;
	@Autowired
	private ISysClassPlant sysClassPlantImpl;
	@Autowired
	private ISysLable sysLableService;
	@Autowired
	private ISys_LearnPlant sys_LearnPlantServiceImpl;

	/**
	 * 增加一个备课
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addSys_LearnPlant")
	public void addSys_LearnPlant(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");

		if (sysUser == null) {
			Msg msg = this.getMsg("0", "没有登陆!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		if ("2".equals(sysUser.getUserType()) || "5".equals(sysUser.getUserType())) {
			if ("2".equals(sysUser.getUserType())) {
				model.setLtype("2");
			} else {
				model.setLtype("5");
			}
			if (null == model.getPlpid() || "".equals(model.getPlpid()) || "0".equals(model.getPlpid())) {
				model.setPlpid("0");
				model.setPltype("1");// 第一级
				model.setpName(model.getName());
			} else {
				model.setpName(model.getpName() + "/" + model.getName());// 自己父级名称
			}
		} else {
			Msg msg = this.getMsg("0", "只要老师和专家才能增加备课!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		model.setSysUser(sysUser);
		String arrclass = request.getParameter("arrclass");// 得到所有的关联的班级
		String arrlable = request.getParameter("arrlable");// //得到当前关联的标签
		Sys_LearnPlant map = null;
		String ptable_id = "";
		if ("4".equals(model.getPltype())) {
			// 先装自己的上级存起来
			map = sys_LearnPlantServiceImpl.addSys_LearnPlant(model, arrclass, arrlable);

			// 和题进行关联
			String testpaper = request.getParameter("testpaper");
			if (null != testpaper && !"".equals(testpaper)) {
				Sys_TestPaper st = (Sys_TestPaper) JSONObject.toBean(JSONObject.fromObject(testpaper), Sys_TestPaper.class);
				map.setSys_TestPaper(st);
				this.sys_LearnPlantServiceImpl.updateLearnPlantHomeWork(map);
			}

			// 更新和模板的关联
			String classC = request.getParameter("classC");
			if (null != classC && !"".equals(classC)) {
				Sys_ClassPlantMaster scm = (Sys_ClassPlantMaster) JSONObject.toBean(JSONObject.fromObject(classC), Sys_ClassPlantMaster.class);
				scm.setSys_LearnPlant(map);
				this.sysClassPlantImpl.updatePlantMasterValue(scm);
			}
			// 继续增加自己的下一级
			String pName = map.getpName();
			String plpid = map.getLpid();
			String children = request.getParameter("children");
			ptable_id = map.getTable_id();
			JSONArray jonArray = JSONArray.fromObject(children);
			if (null != jonArray && null != jonArray.get(0) && !"null".equals(jonArray.get(0))) {
				for (int i = 0; i < jonArray.size(); i++) {
					String jsonstring = jonArray.get(i) + "";
					if ("null".equals(jsonstring)) {
						continue;
					}
					if("".equals(jsonstring)){
						continue;
					}
					JSONObject jsonObject = JSONObject.fromObject(jsonstring);
					// lable标签不为空
					String arrleble = "";
					Sys_LearnPlant lp = new Sys_LearnPlant();

					if (null != jsonObject.get("arrlable") && !"".equals(jsonObject.get("arrlable"))) {
						arrleble = jsonObject.get("arrlable").toString();
					}
					// 1是名称
					if (null != jsonObject.get("buzhou1") && !"".equals(jsonObject.get("buzhou1"))) {
						lp.setName(jsonObject.get("buzhou1").toString());
					}
					// 2是时长
					if (null != jsonObject.get("buzhou2") && !"".equals(jsonObject.get("buzhou2"))) {
						lp.setLearntime(jsonObject.getLong("buzhou2"));
					}
					// 3是场地
					if (null != jsonObject.get("buzhou3") && !"".equals(jsonObject.get("buzhou3"))) {
						lp.setAdress(jsonObject.get("buzhou3").toString());
					}
					// 4是附件
					if (null != jsonObject.get("buzhou4") && !"".equals(jsonObject.get("buzhou4"))) {
						// 目前先不忙附件
					}
					// 5是内容
					if (null != jsonObject.get("buzhou5") && !"".equals(jsonObject.get("buzhou5"))) {
						lp.setContent(jsonObject.get("buzhou5").toString());
					}
					// 6是器材
					if (null != jsonObject.get("buzhou6") && !"".equals(jsonObject.get("buzhou6"))) {
						lp.setJiaoju(jsonObject.get("buzhou6").toString());
					}
					lp.setPlpid(plpid);
					lp.setPltype("5");
					lp.setLtype(sysUser.getUserType());
					lp.setpName(pName + "/" + lp.getName());
					lp.setId1(map.getId1());
					lp.setId2(map.getId2());
					lp.setId3(map.getId3());
					lp.setId4(map.getId4());
					lp.setSysUser(sysUser);
					map = sys_LearnPlantServiceImpl.addSys_LearnPlant(lp, arrclass, arrleble);
				}
			}
		} else {
			map = sys_LearnPlantServiceImpl.addSys_LearnPlant(model, arrclass, arrlable);
		}

		if (null != map) {

			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 查询所有备课
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getSys_LearnPlant")
	public void getSys_LearnPlant(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		Map map = this.sys_LearnPlantServiceImpl.getSys_LearnPlant(model, sysUser);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_LearnPlant);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().flush();
		response.getWriter().close();

	}

	/**
	 * 查询当前最上级的树下面，总共所有的标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getAllLableByPltype1")
	public void getAllLableByPltype1(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		Map map = this.sys_LearnPlantServiceImpl.getAllLableByPltype1(model);

		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_lable);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询当前最上级的树下面，总共所有的标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=deletelearnPlant")
	public void deletelearnPlant(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		int map = this.sys_LearnPlantServiceImpl.deletelearnPlant(model);
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 查询本级有多少个标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getLearnPlantLable")
	public void getLearnPlantLable(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sys_LearnPlantServiceImpl.getLearnPlantLable(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_lable);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询本级有多少个班级
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getLearnPlantClass")
	public void getLearnPlantClass(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sys_LearnPlantServiceImpl.getLearnPlantClass(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_lable);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 修改一个备课
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateSys_LearnPlant")
	public void updateSys_LearnPlant(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (sysUser == null) {
			Msg msg = this.getMsg("0", "没有登陆!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		String arrclass = request.getParameter("arrclass");// 得到所有的关联的班级
		String arrlable = request.getParameter("arrlable");// //得到当前关联的标签

		Sys_LearnPlant map = null;
		if ("4".equals(model.getPltype())) {
			// 先装自己的上级存起来
			map = sys_LearnPlantServiceImpl.updateSys_LearnPlant(model, arrclass, arrlable);
			// 删除下一级的所有，
			int temp = this.sys_LearnPlantServiceImpl.deletelearnPlantChildren(map);
			
			// 和题进行关联
			String testpaper = request.getParameter("testpaper");
			if (null != testpaper && !"".equals(testpaper)) {
				Sys_TestPaper st = (Sys_TestPaper) JSONObject.toBean(JSONObject.fromObject(testpaper), Sys_TestPaper.class);
				map.setSys_TestPaper(st); 
				this.sys_LearnPlantServiceImpl.updateLearnPlantHomeWork(map);
			}

			// 更新和模板的关联
			String classC = request.getParameter("classC");
			if (null != classC && !"".equals(classC)) {
				Sys_ClassPlantMaster scm = (Sys_ClassPlantMaster) JSONObject.toBean(JSONObject.fromObject(classC), Sys_ClassPlantMaster.class);
				scm.setSys_LearnPlant(map);
				this.sysClassPlantImpl.updatePlantMasterValue(scm);
			}
			
			// 将下一级全部加入
			String pName = map.getpName();
			String lpid = map.getLpid();
			String children = request.getParameter("children");
			JSONArray jonArray = JSONArray.fromObject(children);
			if (null != jonArray) {
				for (int i = 0; i < jonArray.size(); i++) {
					String jsonstring = jonArray.get(i) + "";
					JSONObject jsonObject = JSONObject.fromObject(jsonstring);
					if(jsonObject==null || "null".equals(jsonObject+"")){
						continue;
					}
					String str =jsonObject+"";
					// lable标签不为空
					String arrleble = "";
					Sys_LearnPlant lp = new Sys_LearnPlant();

					if (null != jsonObject.get("arrlable") && !"".equals(jsonObject.get("arrlable"))) {
						arrleble = jsonObject.get("arrlable").toString();
					}
					// 1是名称
					if (null != jsonObject.get("buzhou1") && !"".equals(jsonObject.get("buzhou1"))) {
						lp.setName(jsonObject.get("buzhou1").toString());
					}
					// 2是时长
					if (null != jsonObject.get("buzhou2") && !"".equals(jsonObject.get("buzhou2"))) {
						lp.setLearntime(jsonObject.getLong("buzhou2"));
					}
					// 3是场地
					if (null != jsonObject.get("buzhou3") && !"".equals(jsonObject.get("buzhou3"))) {
						lp.setAdress(jsonObject.get("buzhou3").toString());
					}
					// 4是附件
					if (null != jsonObject.get("buzhou4") && !"".equals(jsonObject.get("buzhou4"))) {
						// 目前先不忙附件
					}
					// 5是内容
					if (null != jsonObject.get("buzhou5") && !"".equals(jsonObject.get("buzhou5"))) {
						lp.setContent(jsonObject.get("buzhou5").toString());
					}
					// 6是器材
					if (null != jsonObject.get("buzhou6") && !"".equals(jsonObject.get("buzhou6"))) {
						lp.setJiaoju(jsonObject.get("buzhou6").toString());
					}
					lp.setPlpid(lpid);
					lp.setPltype("5");

					lp.setLtype(sysUser.getUserType());
					lp.setpName(pName + "/" + lp.getName());
					lp.setId1(map.getId1());
					lp.setId2(map.getId2());
					lp.setId3(map.getId3());
					lp.setId4(map.getId4());
					lp.setSysUser(sysUser);
					map = sys_LearnPlantServiceImpl.addSys_LearnPlant(lp, arrclass, arrleble);
				}
			}
		} else {
			map = sys_LearnPlantServiceImpl.updateSys_LearnPlant(model, arrclass, arrlable);
		}
		if (null != map) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 查询自己下一级所有的备课
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getLearnPlantChildren")
	public void getLearnPlantChildren(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		JSONArray map = this.sys_LearnPlantServiceImpl.getLearnPlantChildren(model);
		response.getWriter().write(map.toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询当前这个节点下的所有环节 只有自己和专家的
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getAlllearnPlantBy4")
	public void getAlllearnPlantBy4(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");

		Map map = this.sys_LearnPlantServiceImpl.getAlllearnPlantBy4(model, sysUser);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_LearnPlant);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 环节和课程关联
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateLearnPlantHomeWork")
	public void updateLearnPlantHomeWork(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		if (null == model || null == model.getTable_id() || "".equals(model.getTable_id())) {
			Msg msg = this.getMsg("0", "不存在课程!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		String juanId = request.getParameter("juanId");
		if (null == juanId || null == juanId || "".equals(juanId)) {
			Msg msg = this.getMsg("0", "不存在作业!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Sys_TestPaper st = new Sys_TestPaper();
		st.setTable_id(juanId);
		model.setSys_TestPaper(st);
		int map = this.sys_LearnPlantServiceImpl.updateLearnPlantHomeWork(model);
		if (map == 1) {
			Msg msg = this.getMsg("0", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

	}

	/**
	 * 得到当前登录人，需要上的下一堂课
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getClassBegin")
	public void getClassBegin(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		Map map = this.sys_LearnPlantServiceImpl.getClassBegin(sysUser);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		// jsonConfig.setExcludes(JsonString.sys_LearnPlant);
		System.out.println(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 发放作业
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updatePlantHomeWork")
	public void updatePlantHomeWork(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String sys_PlantId = request.getParameter("sys_PlantId");// 哪个计划
		model.setTable_id(sys_PlantId);
		String classId = request.getParameter("classId");// 哪个班
		Sys_Class sc = new Sys_Class();
		sc.setTable_id(classId);
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		int map = this.sys_LearnPlantServiceImpl.updatePlantHomeWork(model, sc);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
	}

	/**
	 * 完成上课
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updatePlantMasterClose")
	public void updatePlantMasterClose(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String sys_PlantId = request.getParameter("sys_PlantId");// 哪个计划
		model.setTable_id(sys_PlantId);
		String classId = request.getParameter("classId");// 哪个班
		Sys_Class sc = new Sys_Class();
		sc.setTable_id(classId);
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		int map = this.sys_LearnPlantServiceImpl.updatePlantMasterClose(model, sc, sysUser);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().flush();
			response.getWriter().close();
			return;
		} else if (map == 2) {
			Msg msg = this.getMsg("1", "已经在此班上过此课!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
	}
	
 

	/**
	 * 查询这节课在哪个课表上，在课排 表的关联当
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getLearnPlant_masterPlant")
	public void getLearnPlant_masterPlant(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Sys_ClassPlantMaster map = this.sys_LearnPlantServiceImpl.getLearnPlant_masterPlant(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_classPlantMaster);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询这节课关联的环节作业
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getLearnPlant_HomeWork")
	public void getLearnPlant_HomeWork(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Sys_TestPaper map = this.sys_LearnPlantServiceImpl.getLearnPlant_HomeWork(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.test_Paper);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

}
